package com.xiaoxu.privatechat.controller.admin;

import com.xiaoxu.privatechat.bean.CodeMsg;
import com.xiaoxu.privatechat.bean.PageBean;
import com.xiaoxu.privatechat.bean.Result;
import com.xiaoxu.privatechat.entity.common.Account;
import com.xiaoxu.privatechat.service.admin.OperaterLogService;
import com.xiaoxu.privatechat.service.common.AccountService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author xiaoxu
 * @description 后台用户管理控制器
 * @date 2023/2/13
 */
@RequestMapping("/admin/account")
@Controller
public class AccountController {

    @Resource
    private AccountService accountService;

    @Resource
    private OperaterLogService operaterLogService;

    /**
     * @param model    freemarker模型
     * @param account  前台用户信息
     * @param pageBean 分页
     * @return String
     * @description 用户列表页面
     */
    @RequestMapping(value = "/list")
    public String list(Model model, Account account, PageBean<Account> pageBean) {
        model.addAttribute("title", "用戶列表");
        model.addAttribute("username", account.getUsername());
        model.addAttribute("pageBean", accountService.findList(account, pageBean));
        return "admin/account/list";
    }

    /**
     * @param model 模型
     * @return String
     * @description 用户编辑页面
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Model model, @RequestParam(name = "id") Long id) {
        model.addAttribute("user", accountService.find(id));
        return "admin/account/edit";
    }

    /**
     * @param ids 用户id集合
     * @return Result
     * @description 编辑用户
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> edit(@RequestParam(name = "status") Integer status, @RequestParam(name = "ids") Long... ids) {
        if (ids != null) {
            for (Long id : ids) {
                Account account = accountService.find(id);
                if (account != null) {
                    account.setStatus(status);
                    accountService.save(account);
                }
                operaterLogService.add("編輯用戶狀態，用戶：" + id);
            }
            return Result.success(true);
        }
        return Result.error(CodeMsg.DATA_ERROR);
    }

    /**
     * @param account 用户信息
     * @return Result
     * @description 编辑用户
     */
    @RequestMapping(value = "/editAccount", method = RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> editAccount(Account account) {
        Account newAccount = accountService.find(account.getId());
        if (!StringUtils.isBlank(account.getUsername())) {
            newAccount.setUsername(account.getUsername());
        }
        if (!StringUtils.isBlank(account.getPassword())) {
            newAccount.setPassword(account.getPassword());
        }
        if (!StringUtils.isBlank(account.getEmail())) {
            newAccount.setEmail(account.getEmail());
        }
        if (!StringUtils.isBlank(account.getHeadPic())) {
            newAccount.setHeadPic(account.getHeadPic());
        }
        newAccount.setSex(account.getSex());
        newAccount.setStatus(account.getStatus());
        newAccount.setBirthday(account.getBirthday());
        if(accountService.save(newAccount) == null){
            return Result.error(CodeMsg.ACCOUNT_UPDATE_ERROR);
        }
        operaterLogService.add("編輯使用者狀態，用戶ID：" + Objects.requireNonNull(account).getId());
        return Result.success(true);
    }
}
